! This file is part of fpm.
! SPDX-Identifier: MIT
module fpm

    type string_t
        character(len=:), allocatable :: s
    end type

contains

    !> read lines into an array of TYPE(STRING_T) variables
    function read_lines(fh) result(lines)
        integer, intent(in) :: fh
        type(string_t), allocatable :: lines(:)

        integer :: i
        character(1000) :: line_buffer

        allocate (lines(number_of_rows(fh)))
        do i = 1, size(lines)
            read (fh, '(A)') line_buffer
            lines(i)%s = trim(line_buffer)
        end do

    end function read_lines

    !> Determine number or rows in a file given a LUN
    integer function number_of_rows(s) result(nrows)
        integer, intent(in) :: s
        integer :: ios
        rewind (s)
        nrows = 0
        do
            read (s, *, iostat=ios)
            if (ios /= 0) exit
            nrows = nrows + 1
        end do
        rewind (s)
    end function number_of_rows

end module fpm
